import axios from 'axios';
import router from '@/router';
import {
    Message,
    MessageBox,
    Notification
} from 'element-ui';
import urlObj from './baseUrl.js'
// const hostUrl = 'https://cloud.supply.sxruanjian.cn/admin';
// 创建axios实例
const service = axios.create({
    withCredentials: true,
    baseURL: urlObj.hostUrl, // api的base_url
    timeout: 5000 // 请求超时时间
});
// 请求拦截器
service.interceptors.request.use(
    config => {
        config.headers['Authorization'] = localStorage.getItem('Client-Access-Token') ? localStorage.getItem('Client-Access-Token') : '';
        return config;
    },
    error => {
        // 请求错误处理
        console.log(error); // for debug
        Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        // 对响应数据做处理，例如只返回data部分
        const res = response.data;
        if (res.code != 0) {
            if (res.code == 101) { //token失效
                localStorage.removeItem('Client-Access-Token')
                sessionStorage.removeItem('IS-CLOSE');
                // router.push('/login')
            } else {
                Message({
                    message: res.msg,
                    type: 'error'
                })
            }
        }
        return response.data;
    },
    error => {
        console.log('err', error); // for debug
        Message({
            message: '网络不给力,请稍后重试',
            type: 'error',
            duration: 5 * 1000
        });
        return Promise.reject(error);
    }
);

export default service;